package com.pansoft.openplanet.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.frame.baseui.BaseFragment;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.adapter.ResourceAdapter;
import com.pansoft.openplanet.bean.BoxParentBean;
import com.pansoft.openplanet.bean.BoxResourceBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的资源
 *
 * @author YQS
 */

public class MyResourceFragment extends BaseFragment implements View.OnClickListener {

    private View rootView;
    private String type;
    private ExpandableListView expandableListView;
    private List<BoxParentBean> parentBeanList;
    private SmartRefreshLayout refreshLayout;

    private ResourceAdapter mAdapter;


    public MyResourceFragment() {

    }

    public MyResourceFragment(String type) {
        this.type = type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        initView(inflater);
        initData();
        return rootView;
    }

    private void initView(LayoutInflater inflater) {
        rootView = inflater.inflate(R.layout.talk_chain_fragment_my_resource, null);
        expandableListView = rootView.findViewById(R.id.expandListview);
        refreshLayout = rootView.findViewById(R.id.refreshLayout);
        refreshLayout.setEnableAutoLoadMore(false);//开启自动加载功能（非必须）
        refreshLayout.setEnableRefresh(false);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                loadData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore();
            }
        });
    }

    private void initData() {
        parentBeanList = new ArrayList<>();
        mAdapter = new ResourceAdapter(getActivity(), parentBeanList);
        expandableListView.setAdapter(mAdapter);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

        loadData();
    }

    private void loadData() {
        parentBeanList.clear();

        List<BoxResourceBean> list1 = new ArrayList<>();
        List<BoxResourceBean> list2 = new ArrayList<>();
        BoxResourceBean bean1 = new BoxResourceBean.Builder().name("Deskpot.jpg")
                .size("128.3KB").time("2018-04-03 14:56:23")
                .preImage(R.drawable.talk_chain_test_pre1).build();
        list1.add(bean1);

        BoxResourceBean bean2 = new BoxResourceBean.Builder().name("BTH.jpg")
                .size("128.3KB").time("2018-04-03 14:56:23")
                .preImage(R.drawable.talk_chain_test_pre2).build();

        BoxResourceBean bean3 = new BoxResourceBean.Builder().name("Video.mp4")
                .size("24.32MB").time("2018-04-03 14:56:23")
                .preImage(R.drawable.talk_chain_test_pre3).build();
        list1.add(bean2);
        list2.add(bean3);
        BoxParentBean parentBean1 = new BoxParentBean.Builder().count("2")
                .icon(R.drawable.talk_chain_image_icon)
                .name("图片").resourceBeanList(list1)
                .build();
        BoxParentBean parentBean2 = new BoxParentBean.Builder().count("1")
                .icon(R.drawable.talk_chain_video)
                .name("视频").resourceBeanList(list2)
                .build();

        parentBeanList.add(parentBean1);
        parentBeanList.add(parentBean2);
        mAdapter.notifyDataSetChanged();
        for (int a = 0; a < parentBeanList.size(); a++) {
            expandableListView.expandGroup(a);
        }
        refreshLayout.finishRefresh();

    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_tioami) {
//            Intent intent = new Intent(getActivity(), TransferActivity.class);
//            intent.putExtra("myaddress", "");
//            intent.putExtra("balance", "0");
//            intent.putExtra("filePath", "");
//            startActivity(intent);
        }
    }


    /**
     * 显示用户头像
     */
    private void showAvatar(ImageView ivAvatar, String url) {
        RequestOptions options = new RequestOptions()
                .circleCrop().error(R.drawable.contact_default_avatar).placeholder(R.drawable.contact_default_avatar)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(this)
                .load(url)
                .apply(options)
                .into(ivAvatar);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        RefWatcher refWatcher = TalcChainApplication.getRefWatcher(getActivity());
//        refWatcher.watch(this);
    }
}
